package com.Leetcode;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * project : my-learn
 * <p> 这⼀题和第 438 题，第 3 题，第 76 题，第 567 题类似，⽤的思想都是"滑动窗⼝"。 </p>
 * Input: "pwwkew"
 * Output: 3
 * Explanation: The answer is "wke", with the length of 3.
 *  Note that the answer must be a substring, "pwke" is a subsequence
 * and not a substring.
 *
 * @author yisl
 * @date 2022-07-14【星期四】
 **/
public class Leetcode3 {
    //位图解法
    public static int lengthOfLongestSubstring(String s){
        int[] bitArr = new int[256];
        int res=0, left = 0, right = 0;
        for(;left<s.length();){
            if(right<s.length()&&bitArr[s.charAt(right)]==0){
                bitArr[s.charAt(right)]=1;
                right++;
            }else{
                bitArr[s.charAt(left)]=0;
                left++;
            }
            res = Math.max(res, right-left);
        }
        return res;
    }
    //滑动窗口
    public static int lengthOfLongestSubstringForSliding(String s){
        if (s==null || s.length()==0){
            return 0;
        }
        int[] temp = new int[256];
        int res = 0;
        int left=0, right = 0;
        for (;left<s.length();){
            if(right<s.length()&&temp[s.charAt(right)-'a']==0){
                temp[s.charAt(right)-'a']++;
                right++;
            }else{
                temp[s.charAt(left)-'a']--;
                left++;
            }
            res = Math.max(res, right-left);
        }
        return res;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        //String s = "bbbabbbc";
        //System.out.println(lengthOfLongestSubstringForSliding(s));
        //System.out.println("beijing".equalsIgnoreCase(new String("beijing")));
        //SimpleDateFormat format = new SimpleDateFormat("YYYY");
        //
        //
        //readFile("123");
        //ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        //System.out.println(classLoader);
        //System.out.println(classLoader.getParent());
        //System.out.println(classLoader.getParent().getParent());
        //classLoader.loadClass("com.yizn.Leetcode.Test2");
        //
        //Class.forName("com.yizn.Leetcode.Test2");

        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        System.out.println(list.size());
        list.add(1);
        System.out.println(list.size());
        list.add(0, 3);
        System.out.println(list.size());


        LinkedList<String> list2 = new LinkedList<>();
        List<String> list1 = Collections.synchronizedList(list2);

        //HashMap map = new HashMap();
        //map.put()

        Integer i1 = new Integer(123);
        Integer i2= new Integer(123);

        String l1 = new String("abc");
        String l2 = new String("abc");
        System.out.println(i1==i2);
        System.out.println(l1==l2);

    }



    public static void readFile(String filePath)  {
        File file = new File(filePath);
        String result;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            while((result = reader.readLine())!=null) {
                System.out.println(result);
            }
        } catch (IOException e) {
            System.out.println("readFile method catch block.");
            //MyException ex = new MyException("read file failed.");
            //ex.initCause(e);
            //throw ex;
        } finally {
            System.out.println("readFile method finally block.");
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

